package ModelLayer;
import java.util.ArrayList;



public class AddressBook
{
    
    private ArrayList<Person> adBook;

 
    public AddressBook()
    {
     
        adBook=new ArrayList<Person>();
    }

    public void addPerson(Person newPerson)
    {adBook.add(newPerson);}
    public Person getPerson(long id)
    {boolean found=false;
     int index=0;
     while (index<adBook.size()&&!found)
     {Person p=adBook.get(index);
         if (p.getId()==id)
         {found=true;}
         else {index++;}
        }
     if(found) {return adBook.get(index);}
     else {System.out.println("Person not in AddressBook.");return null;}
     
    }
    public void removePerson(long id)
    {boolean found=false;
     int index=0;
     while (index<adBook.size()&&!found)
     {Person p=adBook.get(index);
         if (p.getId()==id)
         {found=true;}
         else {index++;}
        }
     if(found) {Person p=adBook.get(index);
                adBook.remove(p);}
     else {System.out.println("Person not in AddressBook.");}}   
     public ArrayList getPersons()
    {return adBook;
    }
    public void update(long id,String name,String postalCode,String city,String phone,String address)
    {boolean found=false;
     int index=0;
     while (index<adBook.size()&&!found)
     {Person p=adBook.get(index);
         if (p.getId()==id)
         {found=true;}
         else {index++;}
        }
     if(found) {Person p=adBook.get(index);
                p.setName(name);
                p.setCity(city);
                p.setPostalCode(postalCode);
                p.setPhone(phone);
                p.setAddress(address);}}
     public Person find(String name)
     {boolean found=false;
     int index=0;
     while (index<adBook.size()&&!found)
     {Person p=adBook.get(index);
         if (p.getName()==name)
         {found=true;}
         else {index++;}
        }
     if(found) {return adBook.get(index);}
     else {return null;}}
}
